<template>
  <div id="tmpl">
    <!--1.0图片分类-->
    <div id="cate">
      <ul v-bind="{style:'width:'+ulWidth+'px'}">
        <li @click="getImages(0)">全部</li>
        <li v-for="(item,index) in categories" :key='index' @click="getImages(item.id)">{{item.title}}</li>
      </ul>
    </div>
    <!--2.0 图片列表-->
    <div id='imglist'>
      <ul>
        <li v-for="(item,index) in list" :key='index'>
          <router-link v-bind="{to:'/photo/photoinfo/'+item.id}">
            <img v-lazy="item.img_url">
            <div id="desc">
              <h5 v-text="item.title"></h5>
              <p v-text="item.zhaiyao"></p>
            </div>
          </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { Toast } from 'mint-ui';
import common from '../../kits/common.js';
export default {
  data() {
    return {
      ulWidth: 320,//图片分类容器总宽度
      categories: [],//存图片分类数据
      list: [] //存图片数据
    };
  },
  created() {
    //1.0图片分类
    this.getCategories();
    // 2.0 加载图片数据
    var all = 0; //获取所有数据
    this.getImages(all);
  },
  methods: {
    getCategories() {
      const url = common.apidomain + '/api/getimgcategory';
      this.$http.get(url).then(function (response) {

        if (response.body.status != 0) {
          Toast(response.body.message)
          return;
        }
        this.categories = response.body.message;
        //动态生成图片分类容器总宽度
        let w = 62;
        let count = this.categories.length + 1;
        this.ulWidth = w * count;
      })
    },
    getImages(cateid) {
      // 1.0 如果参数没有传递，则默认是0,代表获取所有图片数据
      cateid = cateid || 0;
      // 2.0 确定url
      var url = common.apidomain + '/api/getimages/' + cateid;
      // 3.0 ajax请求数据
      this.$http.get(url).then(function (response) {
        if (response.body.status != 0) {
          Toast(response.body.message);
          return;
        }

        // 4.0 将数据复制给this.list
        this.list = response.body.message;
      });
    }
  }
}
</script>
<style lang="css" scoped>
/*1.0 图片分类*/

#cate {
  width: 375px;
  max-width: 375px;
  overflow-x: auto;
}

#cate ul {
  margin: 0px;
  padding-left: 10px;
}

#cate li {
  cursor: pointer;
  list-style: none;
  display: inline-block;
  color: #0094ff;
  font-size: 14px;
  padding-left: 6px;
}















/*2.0 实现图片列表样式*/

#imglist {}

#imglist ul {
  padding-left: 0px;
}

#imglist li {
  list-style: none;
  position: relative;
}

#imglist img {
  width: 100%;
  height: 300px;
}

#desc {
  width: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  position: absolute;
  bottom: 2px;
  left: 0px;
}

#desc h5 {
  color: #ffffff;
  font-weight: bold;
}

#desc p {
  color: #fff;
}

image[lazy=loading] {
  width: 40px;
  height: 300px;
  margin: auto;
}
</style>